﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;

namespace YRevitJig
{
	public class XAuxiliaryFrameJig : XUiJigBase
	{
		[DllImport("user32.dll")]
		private static extern int SetCursorPos(int x, int y);

		public XAuxiliaryFrameJig.EStage JigStage { get; set; }

		public XAuxiliaryFrameJig(UIDocument uiDoc) : base(uiDoc)
		{
			this.JigStage = XAuxiliaryFrameJig.EStage.eLeftDownPt;
		}

		public XYZ PickAuxiliaryFramePoint(string strPrompt = "请确定立管:")
		{
			XYZ result = null;
			try
			{
				result = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public override void Key(KeyEventArgs keyEventArgs)
		{
			base.Key(keyEventArgs);
			bool flag = keyEventArgs.KeyData == Keys.Space;
			if (flag)
			{
				this.SetJigStage();
			}
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			try
			{
				base.Draw(paintEventArgs);
				bool flag = !this.m_ptCur.IsEmpty;
				if (flag)
				{
					this.xVector = new XYZ(1.0, Math.Tan(this.angle * Math.PI / 180.0), 0.0).Normalize();
					this.yVector = new XYZ(-Math.Tan(this.angle * Math.PI / 180.0), 1.0, 0.0).Normalize();
					XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
					XYZ xyz = this.SetPtCur(base.Client2Revit(this.m_ptCur), NumbericTools.mmToFeet(this.length));
					XCircle2D xcircle2D = new XCircle2D(base.Revit2Client(xyz), XDrawable2D.GetDistance(base.Revit2Client(xyz), base.Revit2Client(xyz + NumbericTools.mmToFeet(this.diameter) * new XYZ(1.0, 0.0, 0.0))) / 2);
					xcircle2D.Draw(g);
					bool flag2 = !this.isAuxiliaryFrame;
					if (!flag2)
					{
						bool flag3 = this.length != 0.0;
						if (flag3)
						{
							XYZ point = xyz.Add(-NumbericTools.mmToFeet(this.length) / 2.0 * this.xVector).Add(NumbericTools.mmToFeet(this.length) / 2.0 * this.yVector);
							XYZ point2 = xyz.Add(-NumbericTools.mmToFeet(this.length) / 2.0 * this.xVector).Add(-NumbericTools.mmToFeet(this.length) / 2.0 * this.yVector);
							XYZ point3 = xyz.Add(NumbericTools.mmToFeet(this.length) / 2.0 * this.xVector).Add(NumbericTools.mmToFeet(this.length) / 2.0 * this.yVector);
							XYZ point4 = xyz.Add(NumbericTools.mmToFeet(this.length) / 2.0 * this.xVector).Add(-NumbericTools.mmToFeet(this.length) / 2.0 * this.yVector);
							XLine2D xline2D = new XLine2D(base.Revit2Client(point), base.Revit2Client(point2));
							xline2D.Draw(g);
							xline2D = new XLine2D(base.Revit2Client(point2), base.Revit2Client(point4));
							xline2D.Draw(g);
							xline2D = new XLine2D(base.Revit2Client(point4), base.Revit2Client(point3));
							xline2D.Draw(g);
							xline2D = new XLine2D(base.Revit2Client(point3), base.Revit2Client(point));
							xline2D.Draw(g);
							bool flag4 = this.block;
							if (flag4)
							{
								this.block = false;
								XAuxiliaryFrameJig.SetCursorPos(XAuxiliaryFrameJig.ScreenCursorPosition.X - 1, XAuxiliaryFrameJig.ScreenCursorPosition.Y);
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		[DllImport("User32.dll")]
		public static extern int GetCursorPos(out System.Drawing.Point point);

		public static System.Drawing.Point ScreenCursorPosition
		{
			get
			{
				System.Drawing.Point result = default(System.Drawing.Point);
				XAuxiliaryFrameJig.GetCursorPos(out result);
				return result;
			}
		}

		private void SetJigStage()
		{
			bool flag = this.JigStage == XAuxiliaryFrameJig.EStage.eCenterPt;
			if (flag)
			{
				this.JigStage = XAuxiliaryFrameJig.EStage.eLeftUpPt;
			}
			else
			{
				bool flag2 = this.JigStage == XAuxiliaryFrameJig.EStage.eLeftUpPt;
				if (flag2)
				{
					this.JigStage = XAuxiliaryFrameJig.EStage.eLeftDownPt;
				}
				else
				{
					bool flag3 = this.JigStage == XAuxiliaryFrameJig.EStage.eLeftDownPt;
					if (flag3)
					{
						this.JigStage = XAuxiliaryFrameJig.EStage.eRightDownOper;
					}
					else
					{
						bool flag4 = this.JigStage == XAuxiliaryFrameJig.EStage.eRightDownOper;
						if (flag4)
						{
							this.JigStage = XAuxiliaryFrameJig.EStage.eRightUpOper;
						}
						else
						{
							bool flag5 = this.JigStage == XAuxiliaryFrameJig.EStage.eRightUpOper;
							if (flag5)
							{
								this.JigStage = XAuxiliaryFrameJig.EStage.eCenterPt;
							}
						}
					}
				}
			}
			XAuxiliaryFrameJig.SetCursorPos(XAuxiliaryFrameJig.ScreenCursorPosition.X + 1, XAuxiliaryFrameJig.ScreenCursorPosition.Y);
			this.block = true;
		}

		private XYZ SetPtCur(XYZ ptCur, double flength)
		{
			bool flag = !this.isAuxiliaryFrame;
			if (!flag)
			{
				bool flag2 = this.JigStage == XAuxiliaryFrameJig.EStage.eLeftUpPt;
				if (flag2)
				{
					ptCur = ptCur.Add(flength / 2.0 * this.xVector).Add(-flength / 2.0 * this.yVector);
				}
				else
				{
					bool flag3 = this.JigStage == XAuxiliaryFrameJig.EStage.eLeftDownPt;
					if (flag3)
					{
						ptCur = ptCur.Add(flength / 2.0 * this.xVector).Add(flength / 2.0 * this.yVector);
					}
					else
					{
						bool flag4 = this.JigStage == XAuxiliaryFrameJig.EStage.eRightDownOper;
						if (flag4)
						{
							ptCur = ptCur.Add(-flength / 2.0 * this.xVector).Add(flength / 2.0 * this.yVector);
						}
						else
						{
							bool flag5 = this.JigStage == XAuxiliaryFrameJig.EStage.eRightUpOper;
							if (flag5)
							{
								ptCur = ptCur.Add(-flength / 2.0 * this.xVector).Add(-flength / 2.0 * this.yVector);
							}
						}
					}
				}
			}
			return ptCur;
		}

		public double length = 0.0;

		public double diameter = 0.0;

		public bool isAuxiliaryFrame = false;

		public double angle = 0.0;

		public XYZ xVector = new XYZ(1.0, 0.0, 0.0);

		public XYZ yVector = new XYZ(0.0, 1.0, 0.0);

		private bool block = false;

		public enum EStage
		{
			eCenterPt,
			eLeftUpPt,
			eLeftDownPt,
			eRightDownOper,
			eRightUpOper
		}
	}
}
